package org.example.smartbuy.controller;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.example.smartbuy.entity.Cart;
import org.example.smartbuy.service.ICartService;
import org.springframework.beans.factory.annotation.Autowired;
import org.example.smartbuy.Result;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 *  购物车前端控制器
 * </p>
 *
 * @since 2024-11-18
 */
@RestController
@RequestMapping("/cart")
@Tag(name = "购物车接口", description = "购物车操作相关接口")
public class CartController {

    @Autowired
    private ICartService cartService;

    /**
     * 添加商品到购物车
     *
     * @param cart 购物车对象
     */
    @Operation(summary = "添加商品到购物车", description = "根据提供的购物车数据添加商品",
            responses = {
                    @ApiResponse(responseCode = "200", description = "操作成功"),
                    @ApiResponse(responseCode = "400", description = "操作失败")
            })
    @PostMapping("/add")
    public Result add(@RequestBody Cart cart) {
        cartService.addToCart(cart);
        return Result.success();
    }

    /**
     * 查询购物车详情
     *
     * @param cartId 购物车ID
     * @return 购物车信息
     */
    @Operation(summary = "查询购物车里商品的详情", description = "根据购物车ID查询购物车详情",
            responses = {
                    @ApiResponse(responseCode = "200", description = "操作成功"),
                    @ApiResponse(responseCode = "400", description = "操作失败")
            })
    @GetMapping("/detail")
    public Result getCartDetail(int cartId) {
        return Result.success(cartService.getCartDetail(cartId));
    }

    /**
     * 更新购物车中的商品数量
     *
     * @param cart   更新后的购物车对象
     */
    @Operation(summary = "更新购物车中的商品数量", description = "根据购物车ID和商品信息更新购物车中商品的数量",
            responses = {
                    @ApiResponse(responseCode = "200", description = "操作成功"),
                    @ApiResponse(responseCode = "400", description = "操作失败")
            })
    @PutMapping("/update")
    public Result updateCartItem(@RequestBody Cart cart) {
        return Result.success(cartService.updateCartItem(cart));
    }

    /**
     * 删除购物车中的商品
     *
     * @param cartId 购物车ID
     */
    @Operation(summary = "删除购物车中的商品", description = "根据购物车ID和商品ID删除购物车中的商品",
            responses = {
                    @ApiResponse(responseCode = "200", description = "操作成功"),
                    @ApiResponse(responseCode = "400", description = "操作失败")
            })
    @Parameter(name = "cartId", description = "购物车ID", required = true)
    @DeleteMapping("/delete")
    public Result deleteCartItem(int cartId) {
        cartService.deleteCartItem(cartId);
        return Result.success();
    }

    /**
     * 查询用户的购物车列表
     *
     * @param userId 用户ID
     * @return 购物车列表
     */
    @Operation(summary = "查询用户购物车列表", description = "根据用户ID查询该用户的购物车列表",
            responses = {
                    @ApiResponse(responseCode = "200", description = "操作成功"),
                    @ApiResponse(responseCode = "400", description = "操作失败")
            })
    @GetMapping("/list")
    public Result getUserCartList(int userId) {
        return Result.success(cartService.getUserCartList(userId));
    }
}
